"""
Case Type   : bugfix
Case Name   : 索引推荐功能测试
Create At   : 2025/6/13
Owner       : liu-tong-8848
Description :
    1、创建表并插入数据
    2、分析表并执行查询语句
    3、设置enable_hypo_index = on并执行explain语句查看索引情况
    4、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
History     :
"""

import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class BugFix(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.user_node = Node('PrimaryDbUser')
        self.constant = Constant()

    def test_dolphin(self):
        self.log.info(f'----{os.path.basename(__file__)} start----')
        text = '----step1:创建表并插入数据；expect:成功----'
        self.log.info(text)
        sql_cmd = f'DROP TABLE IF EXISTS tab_ysl_1; CREATE TABLE tab_ysl_1 (col1 int, col2 int, col3 text);'
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, sql_res, '执行失败' + text)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, sql_res, '执行失败' + text)

        sql_cmd = f'INSERT INTO tab_ysl_1 VALUES(generate_series(1, 300000),generate_series(1, 300000), ' \
                  + f'repeat( chr(int4(random()*26)+65),4));'
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, sql_res, '执行失败' + text)

        text = '----step2:分析表并执行查询语句；expect:成功----'
        self.log.info(text)
        sql_cmd = f'ANALYZE tab_ysl_1;'
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.ANALYZE_SUCCESS_MSG, sql_res, '执行失败' + text)

        sql_cmd = f"SELECT * FROM hypopg_create_index('CREATE INDEX ON tab_ysl_1(col1)');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('btree_tab_ysl_1_col1', sql_res, '执行失败' + text)
        self.assertIn('(1 row)', sql_res, '执行失败' + text)

        text = '----step3:设置enable_hypo_index = on并执行explain语句查看索引情况；expect:成功----'
        self.log.info(text)
        sql_cmd = f"set enable_hypo_index = on; explain SELECT * FROM tab_ysl_1 WHERE col1 = 100;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertIn('Index Scan', sql_res, '执行失败' + text)
        self.assertIn('Index Cond', sql_res, '执行失败' + text)
        self.assertIn('(2 rows)', sql_res, '执行失败' + text)

    def tearDown(self):
        text = '----step5:清理环境；expect:成功----'
        self.log.info(text)
        sql_cmd = f'DROP TABLE IF EXISTS tab_ysl_1;'
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, sql_res, '执行失败' + text)
        self.log.info(f'----{os.path.basename(__file__)} end-----')
